import { createRouter, createWebHashHistory, createWebHistory, RouteRecordRaw } from 'vue-router'

// 自动注入路由
const routeModulesRef:Record<string, any> = import.meta.glob('@/components/RefandReactive/*.vue', { eager: true }) 
// ji算法小练习
const interviewModulesRef:Record<string, any> = import.meta.glob('@/components/interview/*.vue', { eager: true })

// ref路由
let RefRoutes:RouteRecordRaw[]  = [], interviewRoutes:RouteRecordRaw[]  = []; 
// 路由循环
const forRoute = (modules: Record<string, any>)=>{
    let routes: RouteRecordRaw[] = [];
    for (const path in modules) {
        let item = modules[path].default;
        routes.push({
            path: `/${item.__name}`,
            name: `${item.__name}`,
            component: item
        })
    }
    return routes;
}
// 获取ref路由、interview路由
RefRoutes = forRoute(routeModulesRef);
interviewRoutes = forRoute(interviewModulesRef);


const routes: RouteRecordRaw[] = [
    { path: '/', redirect: '/main', name:'' },
    { path: '/main', component: ()=> import('@/components/contentCatalog.vue'), name: 'main' },
    ...RefRoutes,
    { path: '/v-memo', component: ()=> import('@/components/vue-instruction/Vuememo.vue'), name: 'Vuememo' },
    { path: '/interview', name: 'interview', children: interviewRoutes, component: ()=> import('@/components/interview/index.vue')},
]

const router = createRouter({
    // 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
    history: createWebHistory(),
    routes, // `routes: routes` 的缩写
})

export { router }